package com.liu.susu.thread.task.scheduled.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.time.LocalDateTime;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @FileName MyExecutorConfig
 * @Description
 * @Author susu
 * @date 2022-03-07
 **/
@Configuration
public class MyTaskExecutorConfig {

    private int corePoolSize = 5;
    private int maxPoolSize = 8;
    private int queueCapacity = 3;
    private int keepAliveSeconds = 60;

    @Bean
    public Executor getLiuAsyncTaskExecutor() {
        ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor();
        poolTaskExecutor.setCorePoolSize(corePoolSize);
        poolTaskExecutor.setMaxPoolSize(maxPoolSize);
        poolTaskExecutor.setQueueCapacity(queueCapacity);
        poolTaskExecutor.setThreadFactory(Executors.defaultThreadFactory());
        poolTaskExecutor.setKeepAliveSeconds(keepAliveSeconds);
        poolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        poolTaskExecutor.initialize();
        return poolTaskExecutor;
    }

//    @Scheduled(cron = "0/5 * * ? * ?")
//    //@Scheduled(cron = "${susu.task.job.corn}")
//    @Async("getLiuAsyncTaskExecutor")
//    public void taskTest3() {
//        System.out.println("taskTest3()--c>" + Thread.currentThread().getName() + "-->"
//                + LocalDateTime.now());
//    }

}
